/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpers;

import com.configs.ValidatorConfig;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 *
 * @author Administrator
 */
public class Validator {

    /**
     * kiem tra username, phai bao gom cac ky tu tu a -> z; 0 -> 9 va dau "_, ."
     * @param username
     * @return 
     */
    public static boolean checkUsername(String username) {
        Pattern pattern = Pattern.compile(ValidatorConfig.USERNAME_PATTERN);
        Matcher matcher = pattern.matcher(username);
        if (matcher.find()) {
            if (matcher.group().equals(username)) {
                return true;
            }
        }
        return false;
    }   ///// ket thuc
    
    
    /**
     * kiem tra fullName, phai bao gom cac ky tu tu a -> z; 0 -> 9 va dau " "
     * @param fullName
     * @return 
     */

    public static boolean checkFullName(String fullName){
         Pattern pattern = Pattern.compile(ValidatorConfig.FULLNAME_PATTERN);
        Matcher matcher = pattern.matcher(fullName);
        if (matcher.find()) {
            if (matcher.group().equals(fullName)) {
                return true;
            }
        }
        return false;
    }/// ket thuc
    
    
    
    /**
     *  kiem tra so ky tu trong chuoi password
     * @param password
     * @return 
     */
    public static boolean checkPasswordCharacter(String password) {
        if (password.length() > ValidatorConfig.PASSWORD_MIN_CHARACTER
                && password.length() < ValidatorConfig.PASSWORD_MAX_CHARACTER) {
            return true;
        }
        return false;
    }/////// ket thuc

    

    /**
     * check email character
     * @param email
     * @return 
     */
    public static boolean checkEmailCharacter(String email) {
        if (email.length() < ValidatorConfig.EMAIL_MAX_CHARACTER 
                && email.length() > ValidatorConfig.EMAIL_MIN_CHARACTER) {
            return true;
        }
        return false;
    }////ket thuc

    /**
     * kiem tra email
     * @param email
     * @return 
     */
    public static boolean isValidEmailAddress(String email) {
        boolean result = true;
        try {
            InternetAddress emailAddr = new InternetAddress(email);
            emailAddr.validate();
        } catch (AddressException ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }///// ket thuc

    //////// test ham
    public static void main(String[] args) {
//        boolean b = checkSpaceInUsername("cobuongtoi ra");
//        boolean c = isValidEmailAddress("cobuongtoi@.com");
//        Pattern p = Pattern.compile("\\d+");
//        Matcher m = p.matcher("111111111111111111111aa");
//        if (m.find()) {
//            System.out.println(m.group());
//        }

//        System.out.println(b);
//        System.out.println(c);
        boolean a = checkUsername("HEHE555###H_E");
        System.out.println(a);
    }
}
